package com.xnx3.wangmarket.plugin.offlineWeixinNotification;

import java.util.HashMap;
import java.util.Map;

import com.xnx3.BaseVO;
import com.xnx3.DateUtil;
import com.xnx3.j2ee.pluginManage.PluginRegister;
import com.xnx3.j2ee.service.SqlCacheService;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ConsoleUtil;
import com.xnx3.j2ee.util.SpringUtil;
import com.xnx3.kefu.core.bean.ChatUserBean;
import com.xnx3.kefu.core.entity.Kefu;
import com.xnx3.kefu.core.pluginManage.interfaces.MessageInterface;
import com.xnx3.kefu.core.service.KefuService;
import com.xnx3.kefu.core.util.KefuPropertiesUtil;
import com.xnx3.kefu.core.util.KefuUtil;
import com.xnx3.kefu.core.util.SocketUtil;
import com.xnx3.kefu.core.util.UserUtil;
import com.xnx3.kefu.core.vo.MessageReceiveVO;
import com.xnx3.wangmarket.plugin.offlineWeixinNotification.entity.OfflineWeixinNotification;
import com.xnx3.weixin.WeiXinMessageUtil;
import com.xnx3.weixin.WeiXinUtil;

import io.netty.channel.Channel;

/**
 * 离线消息微信公众号推送消息通知
 * @author 管雷鸣
 */
@PluginRegister(applyToCMS = true, menuTitle = "离线通知", menuHref = "/plugin/offlineWeixinNotification/admin/index.jsp", version = "1.0")
public class MessageInterfaceImpl implements MessageInterface{
	
	public static void main(String[] args) {
		
		
//		模板
//		{{first.DATA}}
//		客户姓名：{{keyword1.DATA}}
//		手机号码：{{keyword2.DATA}}
//		咨询时间：{{keyword3.DATA}}
//		{{remark.DATA}}

		Map<String, String> dataMap = new HashMap<String, String>();
		dataMap.put("first", "测试的");
 		dataMap.put("keyword1", "访客xxxx");
 		dataMap.put("keyword2", "无");
 		dataMap.put("keyword3", DateUtil.currentDate("yyyy-MM-dd HH:mm:ss"));
 		dataMap.put("remark", "测试的");
		
//		BaseVO vo = Global.weixin.sendTemplateMessage(templateId,openid, dataMap, null, null);
//		System.out.println(vo);
		
	}
	
	
	@Override
	public void offlineMessage(Channel channel, MessageReceiveVO message) {
		//通过当前的Channel获取通道所属的用户
		ChatUserBean chatUser = SocketUtil.getUser(channel);
		if(chatUser == null){
			//异常的
			return;
		}
		
		Kefu kefu = KefuUtil.getKefu(message.getReceiveId());
		ConsoleUtil.log("message.getReceiveId(): "+message.getReceiveId());
		
		SqlService sqlService = (SqlService) SpringUtil.getBean("sqlService");
		OfflineWeixinNotification noti = sqlService.findAloneByProperty(OfflineWeixinNotification.class, "chatid", message.getReceiveId());
		if(noti == null) {
			//不需要微信推送离线消息
			return;
		}
		
		//发送人
		String sendUserNickname = "";
		ChatUserBean user = UserUtil.getUser(message.getSendId());
		if(user != null) {
			sendUserNickname = user.getNickname();
		}
		
		//消息正文
		String text = message.getText();
		if(text.length() > 121) {
			text = text.substring(0, 120);
		}
		
		//推送离线消息
		Map<String, String> dataMap = new HashMap<String, String>();
		dataMap.put("first", "您的客服平台(kefuid:"+kefu.getId()+")有新的消息");
 		dataMap.put("keyword1", sendUserNickname);
 		dataMap.put("keyword2", "无");
 		dataMap.put("keyword3", DateUtil.currentDate("yyyy-MM-dd HH:mm:ss"));
 		dataMap.put("remark", text);
		
 		String url = KefuPropertiesUtil.getProperty("kefu.api")+"/plugin/offlineWeixinNotification/weixin/chat.do?openid="+noti.getOpenid()+"&zuoxiChatid="+noti.getChatid()+"&otherChatid="+message.getSendId();
 		
		BaseVO vo = Global.weixin.sendTemplateMessage(Global.templateMessageId,noti.getOpenid(), dataMap, url, null);
		ConsoleUtil.log("weixin sendTemplateMessage : "+vo.getInfo());
	}

	@Override
	public void online(Channel channel) {
	}

	@Override
	public void offline(Channel channel) {
	}

	@Override
	public void autoReply(Channel channel, String otherChatId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean messageReceivedChat(Channel channel, MessageReceiveVO msg) {
		// TODO Auto-generated method stub
		return true;
	}
	
	
}
